package com.enqualcomm.kids.mvp.heartrate;

import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.network.socket.response.HeartRateModeQueryResult;
import com.enqualcomm.kids.network.socket.response.HeartRateQueryResult;

import java.util.List;

import rx.Subscriber;

/**
 * Created by Sunzhigang on 2016/7/19.
 */
public class Presenter implements IHeartRateHandler {

    private String terminalid;
    private IHeartRateSettingView settingView;
    private IHeartRateView heartRateView;
    private IHeartRateModel rateModel;
    private AppDefault appDefault;



    public Presenter(String terminalid) {
        this.terminalid = terminalid;
        rateModel = new HeartRateModel();
        appDefault = new AppDefault();
    }

    public void attachSettingView(IHeartRateSettingView settingView) {
        this.settingView = settingView;
    }
    public void disAttachSettingView() {
        this.settingView = null;
    }

    public void attachView(IHeartRateView heartRateView){
        this.heartRateView = heartRateView;
    }

    public void disAttachView(){
        this.heartRateView = null;
    }

    public void updateHeartRateSettings(HeartRateModeQueryResult.Result result){
        rateModel.updateHeartRateSettings(appDefault.getUserkey(),terminalid,result,this);
    }

    public void queryHeartRateSettings(){
        rateModel.getHeartRateSettings(appDefault.getUserkey(),terminalid,this);
    }

    public void queryHeartRate(String date){
        rateModel.queryHeartRate(appDefault.getUserkey(),terminalid,date,this);
    }

    public void remoteQueryHeartRate(){
        HeartRateClient client = new HeartRateClient(appDefault.getUserkey(),appDefault.getUserid(),terminalid);
        client.queryHeartRate().subscribe(new Subscriber<HeartRateQueryResult.Result>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(HeartRateQueryResult.Result result) {
                if (heartRateView != null){
                    heartRateView.updateAutoHeartRate(result);
                }
            }
        });
    }

    @Override
    public void getHeartRateSttingsSuccess(HeartRateModeQueryResult.Result result) {
        if (settingView!=null) {
            settingView.updateSettings(result);
        }
    }

    @Override
    public void setHeartRateSttingsSuccess() {
        if (settingView!=null) {
            settingView.success();
        }
    }

    @Override
    public void queryHeartRateSuccess(List<HeartRateQueryResult.Result> results) {
        if (heartRateView != null){
            heartRateView.updateHeartRate(results);
        }

    }

    @Override
    public void error(int code, String desc) {
        if (settingView != null){
            settingView.error();
        }

        if (heartRateView != null){
            heartRateView.error();
        }
    }
}
